package com.artwork.controller.api;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Address;
import com.artwork.model.User;
import com.artwork.service.AddressService;
import com.artwork.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author JoJo
 * @Data 2021/9/9$ 10:14$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/api/address")
public class AddressApiController {

    @Autowired
    AddressService addressService;
    @Autowired
    UserService userService;


    /**
     * @desc 我的地址
     * @param id
     * @return
     * @throws JsonException
     */
    @GetMapping("/")
    public List<Address> myAddress(int id) throws JsonException {
        User user = userService.getUserById(id);
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        List<Address> addressList = addressService.getMyAddress(id);
        return addressList;
    }

    /**
     * @desc 新增地址
     * @param address
     * @return
     * @throws JsonException
     */
    @PostMapping("/create")
    public AjaxResult newAddress(Address address) throws JsonException {
        User user = userService.getUserById(address.getUserId());
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
//        修改其他地址保持默认唯一
        if (address.getType() == 1){
            List<Address> addressList = addressService.getMyAddress(address.getUserId());
            if (addressList.size() != 0){
                int type = 0;
                for (int i = 0; i < addressList.size(); i++){
                    addressService.updateAddressType(addressList.get(i).getId(), type);
                }
            }
        }
        addressService.newAddress(address);
        return AjaxResult.success();
    }

    /**
     * @desc 修改地址
     * @param address
     * @return
     * @throws JsonException
     */
    @PostMapping("/update")
    public AjaxResult updateAddress(Address address) throws JsonException {
        User user = userService.getUserById(address.getUserId());
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }

//        设置默认地址
        if (address.getType() == 1){
            List<Address> addressList = addressService.getMyAddress(address.getUserId());
            if (addressList.size() >1){
                int type = 0;
                for (int i = 0; i < addressList.size(); i++){
                    addressService.updateAddressType(addressList.get(i).getId(), type);
                }
            }
        }
        addressService.updateAddressType(address);

        return AjaxResult.success();
    }

    /**
     * @desc 删除地址
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/delete")
    public AjaxResult deleteAddress(int id) throws JsonException {
        Address address = addressService.getAddressById(id);
        if (address == null) {
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        addressService.delete(id);
        return AjaxResult.success();
    }
}
